/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.launch.swing;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.io.IOException;
import java.net.URL;

/**
 * SplashScreenAdaptor : The adaptor for the Java splash screen.
 * 
 * @author Romain DUBOIS
 */
public class SplashScreenAdaptor implements ISplashScreen {

    /** The Java splashscreen */
    private final SplashScreen mSplashScreen;
    /** The last progress */
    private int mLast;

    /** The color used to paint empty progress bar */
    private Color mProgressEmptyColor;
    /** The color used to paint filled progress bar */
    private Color mProgressFilledColor;
    /** The color used to paint text in empty progress bar */
    private Color mTextEmptyColor;
    /** The color used to paint text in filled progress bar */
    private Color mTextFilledColor;

    /**
     * Constructor
     * 
     * @param pSplashScreen
     *            The Java splashscreen
     */
    public SplashScreenAdaptor(final SplashScreen pSplashScreen) {
        super();

        mSplashScreen = pSplashScreen;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setProgress(int, java.lang.String)
     */
    @Override
    public void setProgress(final int pValue, final String pMessage) {
        if (!mSplashScreen.isVisible()) {
            return;
        }
        mLast = Math.max(mLast, pValue);

        final int lWidth = mSplashScreen.getBounds().width;
        final int lHeight = mSplashScreen.getBounds().height;
        final int lBarHeight = 20;
        final Graphics2D lGraphics = mSplashScreen.createGraphics();

        // Clear area
        lGraphics.setComposite(AlphaComposite.Clear);
        lGraphics.fillRect(0, lHeight - 30, lWidth, 30);

        lGraphics.setPaintMode();

        // Draw progress bar
        lGraphics.setColor(mProgressEmptyColor);
        lGraphics.fillRect(5, lHeight - 5 - lBarHeight, lWidth - 10, lBarHeight);
        lGraphics.setColor(Color.GRAY);
        lGraphics.drawRect(5, lHeight - 5 - lBarHeight, lWidth - 10, lBarHeight);
        lGraphics.setColor(mProgressFilledColor);
        lGraphics.fillRect(6, lHeight - 4 - lBarHeight, ((lWidth - 11) * mLast) / 100,
                lBarHeight - 1);

        // Print dark message (on white background)
        lGraphics.setFont(lGraphics.getFont().deriveFont(Font.BOLD));
        final FontMetrics lFontMetrics = lGraphics.getFontMetrics();
        final int lTextWidth = lFontMetrics.stringWidth(pMessage);
        lGraphics.setColor(mTextEmptyColor);
        lGraphics.drawString(pMessage, Math.max(5, (lWidth - lTextWidth) / 2), lHeight - 11);

        // Print light messages (on the progress background)
        lGraphics.setColor(mTextFilledColor);
        lGraphics.clipRect(5, lHeight - 5 - lBarHeight, ((lWidth - 9) * mLast) / 100, lHeight - 1);
        lGraphics.drawString(pMessage, Math.max(5, (lWidth - lTextWidth) / 2), lHeight - 11);

        mSplashScreen.update();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#close()
     */
    @Override
    public synchronized void close() {
        if (mSplashScreen.isVisible()) {
            mSplashScreen.close();
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setBackgroundImage(java.net.URL)
     */
    @Override
    public void setBackgroundImage(final URL pImageUrl) throws IOException {
        mSplashScreen.setImageURL(pImageUrl);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setEmptyProgressColor(java.awt.Color)
     */
    @Override
    public void setEmptyProgressColor(final Color pColor) {
        mProgressEmptyColor = pColor;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setFilledProgressColor(java.awt.Color)
     */
    @Override
    public void setFilledProgressColor(final Color pColor) {
        mProgressFilledColor = pColor;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setEmptyTextColor(java.awt.Color)
     */
    @Override
    public void setEmptyTextColor(final Color pColor) {
        mTextEmptyColor = pColor;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setFilledTextColor(java.awt.Color)
     */
    @Override
    public void setFilledTextColor(final Color pColor) {
        mTextFilledColor = pColor;
    }
}
